package com.wss.module.market.ui.goods.cart.adapter;

import com.wss.common.base.adapter.BaseListAdapter;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.base.adapter.superadapter.SuperViewHolder;
import com.wss.common.bean.Event;
import com.wss.common.constants.EventAction;
import com.wss.common.utils.EventBusUtils;
import com.wss.common.utils.ImageUtils;
import com.wss.common.widget.CountClickView;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.GoodsInfo;
import com.wss.module.market.bean.Vendor;
import com.wss.module.market.utils.ShoppingCartUtils;
import ohos.agp.components.*;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ShoppingCartAdapter extends BaseListAdapter<Vendor> implements Component.ClickedListener {

    private Context context;

    private boolean isEdit;

    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataChanged();
    }

    /**
     * 常规列表重写该方法
     *
     * @param context     context
     * @param list        数据源
     * @param listener    Item点击回调
     */
    public ShoppingCartAdapter(Context context, List<Vendor> list, OnListItemClickListener<Vendor> listener) {
        super(context, list, ResourceTable.Layout_market_item_of_shopping_cart_list, listener);
        this.context = context;
    }

    @Override
    public void onBindData(@NotNull SuperViewHolder holder, int layoutPosition, @NotNull Vendor data) {
        holder.setText(ResourceTable.Id_tv_vendor_name, data.getVendorName());
        DirectionalLayout goodsLayout = holder.findViewById(ResourceTable.Id_goods_list);
        Image ivVendor = holder.findViewById(ResourceTable.Id_iv_check_vendor);
        ivVendor.setSelected(data.isChecked());
        if (ivVendor.isSelected()) {
            ivVendor.setBackground(ImageUtils
                    .getPixelById(context,
                            ResourceTable.Media_market_icon_checkbox_checked));
        } else {
            ivVendor.setBackground(ImageUtils
                    .getPixelById(context,
                            ResourceTable.Media_market_icon_checkbox_default));
        }
        ivVendor.setTag(layoutPosition);
        ivVendor.setClickedListener(this);
        goodsLayout.removeAllComponents();
        for (int i = 0; i < data.getGoodsInfos().size(); i++) {

            Component view = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_market_item_of_shopping_cart_list_goods_list,
                            null,
                            false);
            new ViewHolder(view).onBindData(layoutPosition, i);
            goodsLayout.addComponent(view);
        }
    }

    @Override
    public void onClick(Component v) {
        if (v.getId() == ResourceTable.Id_iv_check_vendor) {
            //供应商被点击了
            ShoppingCartUtils.checkVendor(getData(), (Integer) v.getTag());
            updateCart();
        } else if (v.getId() == ResourceTable.Id_iv_goods_check) {
            //商品被点击了
            String[] tag = ((String) v.getTag()).split(",");
            ShoppingCartUtils.checkGoods(getData(), Integer.parseInt(tag[0]), Integer.parseInt(tag[1]));
            updateCart();
        }
    }

    /**
     * 商品View
     */
    public class ViewHolder {
        Image ivGoods;

        Image ivGoodsCheck;

        Text tvGoodsName;

        Text tvPrice;

        CountClickView ccvClick;

        ViewHolder(Component itemView) {
            ivGoods = (Image) itemView.findComponentById(ResourceTable.Id_iv_goods);
            ivGoodsCheck = (Image) itemView.findComponentById(ResourceTable.Id_iv_goods_check);
            tvGoodsName = (Text) itemView.findComponentById(ResourceTable.Id_tv_goods_name);
            tvPrice = (Text) itemView.findComponentById(ResourceTable.Id_tv_price);
            ccvClick = (CountClickView) itemView.findComponentById(ResourceTable.Id_ccv_click);
        }

        void onBindData(final int parent, final int child) {
            final GoodsInfo goodsInfo = getData().get(parent).getGoodsInfos().get(child);
            if (goodsInfo != null) {

                ImageUtils.loadImage(ivGoods, goodsInfo.getGoodsMasterImg());
                ivGoodsCheck.setSelected(goodsInfo.isChecked());
                if (ivGoodsCheck.isSelected()) {
                    ivGoodsCheck.setBackground(ImageUtils
                            .getPixelById(context,
                                    ResourceTable.Media_market_icon_checkbox_checked));
                } else {
                    ivGoodsCheck.setBackground(ImageUtils
                            .getPixelById(context,
                                    ResourceTable.Media_market_icon_checkbox_default));
                }
                ivGoodsCheck.setTag(parent + "," + child);
                ivGoodsCheck.setClickedListener(ShoppingCartAdapter.this);
                tvGoodsName.setText(goodsInfo.getGoodsName());
                tvPrice.setText(String.format("¥%s", goodsInfo.getGoodsPrice()));
                ccvClick.setMinCount(1);
                ccvClick.setMaxCount(99);
                ccvClick.setInput(true);
                ccvClick.setCurrCount(Math.max(goodsInfo.getNum(), 1));
                ccvClick.setAfterClickListener((view, value) -> {
                    //修改源数据
                    getData().get(parent).getGoodsInfos().get(child).setNum(value);
                    //修改本地数据库数据
                    goodsInfo.setNum(value);
                    ShoppingCartUtils.updateCartGoodsNum(goodsInfo);
                    updateCart();
                });
            }

            tvPrice.setVisibility(isEdit ? Component.HIDE : Component.VISIBLE);
            ccvClick.setVisibility(isEdit ? Component.HIDE : Component.VISIBLE);
        }
    }

    private void updateCart() {
        //刷新购物车
        EventBusUtils.sendEvent(new Event(EventAction.EVENT_SHOPPING_CART_CHANGED));
    }
}
